var mongo = require('mongodb');
 
var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;
 
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('avicultorDB', server);
 
db.open(function(err, db) {
    if(!err) {
        console.log("Connected to 'avicultoresdb' database");/*
        db.collection('wines', {strict:true}, function(err, collection) {
            if (err) {
                console.log("The 'avicultoresdb' collection doesn't exist. Creating it with sample data...");
                //populateDB();
            }
        });*/
    }
});



exports.agregarFormulario = function(datos,callback){

    db.collection('avicultoresFormulario', function(err, collection) {
        collection.insert(datos, {safe:true}, function(err, result) {
            if (err) {
				callback(false, null);
                //res.send({'error':'An error has occurred'});
            } else {
                console.log('Success: ' + JSON.stringify(result[0]));
                callback(true, result[0]);
                //res.send(result[0]);
            }
        });
    });
    
};


exports.agregarFormularioVerificado = function(id,estado,datosAgregar,callback){

    db.collection('avicultoresFormulario', function(err, collection) {
        collection.update({'_id':new BSON.ObjectID(id)}, {
     '$set': { 'formularioVerificacion':datosAgregar,'estadoDenuncia':estado }},{safe:true}, function(err, result) {
            if (err) {
				callback(false, null);
                //res.send({'error':'An error has occurred'});
            } else {
                console.log('Success_asdasd: ' + JSON.stringify(result));
                callback(true, result);
                //res.send(result[0]);
            }
        });
    });
    
};


exports.login = function(usuario,callback){

console.log('usuario: '+JSON.stringify(usuario));
        db.collection('fiscalizadores', function(err, collection) {
        collection.find(usuario).toArray(function(err, items) {
        
             if (err) {
				callback(false, null);
                //res.send({'error':'An error has occurred'});
            } else {
                console.log('Success: ' + JSON.stringify(items));
                if(items.length>0)
                	callback(true, items[0]);
                else
                	callback(false, null);
                //res.send(result[0]);
            }
        
            //res.send(items);
        });
    });
    
};


exports.denunciaFiscalizar = function(usuario,callback){

        db.collection('fiscalizadores', function(err, collection) {
        collection.find(usuario).toArray(function(err, items) {
        
             if (err) {
				callback(false, null);
                //res.send({'error':'An error has occurred'});
            } else {
                console.log('Success: ' + JSON.stringify(items));
                if(items.length>0)
                	callback(true, items[0].fiscalizar);
                else
                	callback(false, null);
                //res.send(result[0]);
            }
        
            //res.send(items);
        });
    });
    
};






exports.agregarImagenesFormulario = function(datos,callback){

    db.collection('avicultoresImagenes', function(err, collection) {
        collection.insert(datos, {safe:true}, function(err, result) {
            if (err) {
				callback(false, null);
                //res.send({'error':'An error has occurred'});
            } else {
                console.log('Success: ' + JSON.stringify(result[0]));
                callback(true, result[0]);
                //res.send(result[0]);
            }
        });
    });
    
};


